home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / smail-3.1.28 / pd / pathalias / makedb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-12  |  2.5 KB  |  143 lines

  1. /* Smail SCCS ID: @(#)pd/pathalias/makedb.c    1.5 %G 11:14:17 */
  2. /* pathalias -- by steve bellovin, as told to peter honeyman */
  3. #ifndef lint
  4. static char    *sccsid = "@(#)makedb.c    9.1 87/10/04";
  5. #endif /* lint */
  6.  
  7. #include <stdio.h>
  8. #include "config.h"
  9.  
  10. #ifdef SMAIL_3
  11. #include "dbm_compat.h"
  12. #else
  13. typedef struct {
  14.     char *dptr;
  15.     int dsize;
  16. } datum;
  17. #endif
  18.  
  19. char *Ofile = ALIASDB, *ProgName;
  20.  
  21. #define USAGE "%s [-o dbmname] [-a] [file ...]\n"
  22.  
  23. main(argc, argv)
  24.     char *argv[];
  25. {    char *ofptr;
  26.     int c, append = 0;
  27.     extern int optind;
  28.     extern char *optarg;
  29.  
  30.     ProgName = argv[0];
  31.     while ((c = getopt(argc, argv, "o:a")) != EOF)
  32.         switch(c) {
  33.         case 'o':    /* dbm output file */
  34.             Ofile = optarg;
  35.             break;
  36.  
  37.         case 'a':    /* append mode */
  38.             append++;
  39.             break;
  40.  
  41.         default:
  42.             fprintf(stderr, USAGE, ProgName);
  43.             exit(1);
  44.             break;
  45.         }
  46.  
  47.  
  48.     if ((ofptr = rindex(Ofile, '/')) != 0)
  49.         ofptr++;
  50.     else
  51.         ofptr = Ofile;
  52. #if defined(SMAIL_3) && defined(ANSI_C) && !defined(NO_SIZE_T_STRLEN)
  53.     if (strlen(ofptr) > (size_t)10)
  54. #else
  55.     if (strlen(ofptr) > 10)
  56. #endif
  57.     {
  58.         ofptr[10] = 0;
  59.         fprintf(stderr, "%s: using %s for dbm output\n", ProgName, Ofile);
  60.     }
  61.  
  62.     if (append == 0 && dbfile(Ofile) != 0) {
  63.         perror_(Ofile);
  64.         exit(1);
  65.     }
  66.  
  67.     if (dbminit(Ofile) < 0) {
  68.         perror_(Ofile);
  69.         exit(1);
  70.     }
  71.  
  72.     if (optind == argc)
  73.         makedb((char *) 0);
  74.     else for ( ; optind < argc; optind++)
  75.         makedb(argv[optind]);
  76.  
  77.     dbmclose();
  78.  
  79.     exit(0);
  80. }
  81.  
  82. dbfile(dbf)
  83.     char *dbf;
  84. {
  85.     return (dbcreat(dbf, "dir") != 0 || dbcreat(dbf, "pag") != 0);
  86. }
  87.  
  88. dbcreat(dbf, suffix)
  89.     char *dbf, *suffix;
  90. {    char buf[BUFSIZ];
  91.     int fd;
  92.  
  93.     (void) sprintf(buf, "%s.%s", dbf, suffix);
  94.     if ((fd = creat(buf, 0666)) < 0)
  95.         return(-1);
  96.     (void) close(fd);
  97.     return(0);
  98. }
  99.  
  100.  
  101. makedb(ifile)
  102.     char *ifile;
  103. {    char line[BUFSIZ];
  104.     datum key, val;
  105.  
  106.     if (ifile && (freopen(ifile, "r", stdin) == NULL)) {
  107.         perror_(ifile);
  108.         return;
  109.     }
  110.  
  111.     /*
  112.      * keys and values are 0 terminated.  this wastes time and (disk) space,
  113.      * but does lend simplicity and backwards compatibility.
  114.      */
  115.     key.dptr = line;
  116.     while (fgets(line, sizeof(line), stdin) != NULL) {
  117.         char *op, *end;
  118.  
  119.         end = line + strlen(line);
  120.         end[-1] = 0;    /* kill newline, stuff null terminator */
  121.         op = index(line, '\t');
  122.         if (op != 0) {
  123.             *op++ = 0;
  124.             key.dsize = op - line;        /* 0 terminated */
  125.             val.dptr = op;
  126.             val.dsize = end - op;        /* 0 terminated */
  127.         } else {
  128.             key.dsize = end - line;        /* 0 terminated */
  129.             val.dptr = "\0";        /* why must i do this? */
  130.             val.dsize = 1;
  131.         }
  132.         if (store(key, val) < 0)
  133.             perror_(Ofile);
  134.     }
  135. }
  136.  
  137. perror_(str)
  138.     char    *str;
  139. {
  140.     fprintf(stderr, "%s: ", ProgName);
  141.     perror(str);
  142. }
  143.